package com.promise.controller;

import com.promise.service.PrometheusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@CrossOrigin
public class PrometheusController {

    private final PrometheusService prometheusService;

    @Autowired
    public PrometheusController(PrometheusService prometheusService) {
        this.prometheusService = prometheusService;
    }

    @GetMapping("/prometheus/query")
    public String queryPrometheus(@RequestParam(defaultValue = "0") long start,
                                  @RequestParam(defaultValue = "0") long end,
                                  @RequestParam(defaultValue = "1m") String step) {
        // 如果没有提供时间戳参数，默认使用当前时间
        if (start == 0) {
            start = System.currentTimeMillis() / 1000;
        }
        if (end == 0) {
            end = System.currentTimeMillis() / 1000;
        }

        try {
            return prometheusService.queryPrometheus(start, end, step);
        } catch (IOException e) {
            e.printStackTrace();
            return "Error querying Prometheus: " + e.getMessage();
        }
    }


    @GetMapping("/prometheus/memory")
    public String queryMemoryUsage(@RequestParam(defaultValue = "0") long start,
                                   @RequestParam(defaultValue = "0") long end,
                                   @RequestParam(defaultValue = "1m") String step) {
        // 如果没有提供时间戳参数，默认使用当前时间
        if (start == 0) {
            start = System.currentTimeMillis() / 1000;
        }
        if (end == 0) {
            end = System.currentTimeMillis() / 1000;
        }

        try {
            return prometheusService.queryMemoryUsage(start, end, step);
        } catch (IOException e) {
            e.printStackTrace();
            return "Error querying Prometheus: " + e.getMessage();
        }
    }

    @GetMapping("/prometheus/disk")
    public String querydiskUsage(@RequestParam(defaultValue = "0") long start,
                                 @RequestParam(defaultValue = "0") long end,
                                 @RequestParam(defaultValue = "1m") String step) {
        // 如果没有提供时间戳参数，默认使用当前时间
        if (start == 0) {
            start = System.currentTimeMillis() / 1000;
        }
        if (end == 0) {
            end = System.currentTimeMillis() / 1000;
        }

        try {
            return prometheusService.querydiskUsage(start, end, step);
        } catch (IOException e) {
            e.printStackTrace();
            return "Error querying Prometheus: " + e.getMessage();
        }
    }

    @GetMapping("/prometheus/network")
    public String querynetworkUsage(@RequestParam(defaultValue = "0") long start,
                                    @RequestParam(defaultValue = "0") long end,
                                    @RequestParam(defaultValue = "1m") String step) {
        // 如果没有提供时间戳参数，默认使用当前时间
        if (start == 0) {
            start = System.currentTimeMillis() / 1000;
        }
        if (end == 0) {
            end = System.currentTimeMillis() / 1000;
        }

        try {
            return prometheusService.querynetworkUsage(start, end, step);
        } catch (IOException e) {
            e.printStackTrace();
            return "Error querying Prometheus: " + e.getMessage();
        }
    }

    @GetMapping("/prometheus/netstatwait")
    public String querynetstatwaitUsage(@RequestParam(defaultValue = "0") long start,
                                        @RequestParam(defaultValue = "0") long end,
                                        @RequestParam(defaultValue = "1m") String step) {
        // 如果没有提供时间戳参数，默认使用当前时间
        if (start == 0) {
            start = System.currentTimeMillis() / 1000;
        }
        if (end == 0) {
            end = System.currentTimeMillis() / 1000;
        }

        try {
            return prometheusService.querynetstatwaitUsage(start, end, step);
        } catch (IOException e) {
            e.printStackTrace();
            return "Error querying Prometheus: " + e.getMessage();
        }
    }

    @GetMapping("/prometheus/losspk")
    public String querylosspkUsage(@RequestParam(defaultValue = "0") long start,
                                   @RequestParam(defaultValue = "0") long end,
                                   @RequestParam(defaultValue = "1m") String step) {
        // 如果没有提供时间戳参数，默认使用当前时间
        if (start == 0) {
            start = System.currentTimeMillis() / 1000;
        }
        if (end == 0) {
            end = System.currentTimeMillis() / 1000;
        }

        try {
            return prometheusService.querylosspkUsage(start, end, step);
        } catch (IOException e) {
            e.printStackTrace();
            return "Error querying Prometheus: " + e.getMessage();
        }
    }

    @GetMapping("/prometheus/timeping")
    public String querytimepingUsage(@RequestParam(defaultValue = "0") long start,
                                     @RequestParam(defaultValue = "0") long end,
                                     @RequestParam(defaultValue = "1m") String step) {
        // 如果没有提供时间戳参数，默认使用当前时间
        if (start == 0) {
            start = System.currentTimeMillis() / 1000;
        }
        if (end == 0) {
            end = System.currentTimeMillis() / 1000;
        }

        try {
            return prometheusService.querytimepingUsage(start, end, step);
        } catch (IOException e) {
            e.printStackTrace();
            return "Error querying Prometheus: " + e.getMessage();
        }
    }
}
